<?php

/**
 * This is the model class for table "{{city}}".
 *
 * The followings are the available columns in table '{{city}}':
 * @property string $id
 * @property string $country_id
 * @property string $region_id
 * @property string $name
 * @property string $oreder
 * @property string $url
 * @property string $lat
 * @property string $lng
  * @property integer is_reg_center
 * @property integer $is_active
 */
class City extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return City the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{city}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('is_active', 'required'),
            array('is_reg_center, is_active, oreder', 'numerical', 'integerOnly' => true),
            array('country_id, oreder', 'length', 'max' => 11),
            array('region_id', 'length', 'max' => 10),
            array('name', 'length', 'max' => 128),
            array('url, lat, lng', 'length', 'max' => 254),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, country_id, region_id, name, oreder, url, lat, lng, is_reg_center, is_active', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'сountry' => array(self::BELONGS_TO, 'Country', 'country_id'),
            'region' => array(self::BELONGS_TO, 'Region', 'region_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'country_id' => 'Страна',
            'region_id' => 'регион',
            'name' => 'Название',
            'oreder' => 'Порядок',
            'is_reg_center' => 'Региональный центр',
            'is_active' => 'Активность',
            'url' => 'url',
            'lat' => 'lat',
            'lng' => 'lng',
        );
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            
            $page = Page::getPageFromUrl($this->url);
            $page->is_active = $this->is_active;
            $page->save();
            
            return true;
        }
        else
            return false;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('country_id', $this->country_id, true);
        $criteria->compare('region_id', $this->region_id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('oreder', $this->oreder, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('lat', $this->lat, true);
        $criteria->compare('lng', $this->lng, true);
        $criteria->compare('is_reg_center', $this->is_reg_center);
        $criteria->compare('is_active', $this->is_active);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function isActive() {
        return $this->is_active ? 'Активна' : 'Не aктивна';
    }
        
    public function isRegCenter() {
        return $this->is_reg_center ? 'Да' : 'Нет';
    }
    
    public function getActiveCity() {

        $criteria = new CDbCriteria;
        $criteria->condition = 'country_id = 9908 AND is_active = 1';
        $criteria->order = 'oreder DESC';
        return City::model()->findAll($criteria);
    }

    public function getCityFromUrl($url) {

        $criteria = new CDbCriteria;
        $criteria->condition = 'url = :url';
        $criteria->params = array(':url' => $url);
        return City::model()->find($criteria);
    }

}